package com.houtai.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.houtai.commonutils.JwtUtils;
import com.houtai.commonutils.Result;
import com.houtai.eduorder.entity.Order;
import com.houtai.eduorder.entity.vo.OrderQuery;
import com.houtai.eduorder.service.OrderService;
import com.houtai.eduservice.config.OperationLogDesc;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author xst
 * @since 2022-12-14
 */
@RestController
@RequestMapping("/eduorder/order")
//@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     *生成订单的方法
     * @param courseId
     * @param request
     * @return
     */
    @PostMapping("createOrder/{courseId}")
    public Result saveOrder(@Valid @PathVariable Integer courseId,HttpServletRequest request){
        //从token中获取用户信息
        //创建订单，返回订单号
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return Result.ok().data("orderId",orderNo);
    }

    /**
     * 根据订单号查询订单信息
     * @param orderId
     * @return
     */
    @PostMapping("getOrderInfo/{orderId}")
    public Result getOrderInfo(@Valid @PathVariable String orderId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        return Result.ok().data("item",order);
    }
    //4.多条件组合查询带分页
    @ApiOperation(value = "多条件组合查询带分页")
    @OperationLogDesc(module = "订单模块",description = "查询订单列表")
    @PostMapping("pageCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable long current,
                                       @PathVariable long limit,
                                       @RequestBody(required = false) OrderQuery orderQuery){
        //创建page对象
        Page<Order> pageT = new Page<>(current,limit);
        //调用方法实现分页
        //构建条件
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        //wrapper
        //动态sql
        String courseTitle=orderQuery.getCourseTitle();
        String teacher=orderQuery.getTeacherName();
        String mobile=orderQuery.getMobile();
        Integer payType=orderQuery.getPayType();
        Integer status=orderQuery.getStatus();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(courseTitle)){
            //构建条件
            wrapper.like("courseTitle",courseTitle);

        }
        if(!StringUtils.isEmpty(teacher)){
            //构建条件
            wrapper.like("teacher",teacher);

        }
        if(!StringUtils.isEmpty(mobile)){
            //构建条件
            wrapper.like("mobile",mobile);

        }
        if(!StringUtils.isEmpty(payType)){
            //构建条件
            wrapper.eq("payType",payType);

        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        //排序

        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        orderService.page(pageT,wrapper);

        long total = pageT.getTotal();//总记录数
        List<Order> records = pageT.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }


}

